from accounts.models import UserAccount
from django.db import models


class PersonalityTag(models.Model):
    """
    个性标签模型，用于存储用户的个性标签信息
    """
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name


class UserInform(models.Model):
    """
    用户信息模型，用于存储额外的用户信息
    """
    user = models.ForeignKey(UserAccount, on_delete=models.CASCADE)
    nickname = models.CharField(
        max_length=100, blank=True, null=True, default="momo")
    bio = models.TextField(blank=True, null=True, default='这个人很懒，什么也没有留下')
    avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)
    background_image = models.ImageField(
        upload_to='backgrounds/', blank=True, null=True)
    total_likes = models.IntegerField(default=0)
    likes_received = models.ManyToManyField(
        'self', through='Like', symmetrical=False, related_name='likes_sent')
    personality_tags = models.ManyToManyField(PersonalityTag, blank=True)


class Like(models.Model):
    """
    点赞模型，用于表示用户之间的点赞关系
    """
    sender = models.ForeignKey(
        UserInform, on_delete=models.CASCADE, related_name='sent_likes')
    receiver = models.ForeignKey(
        UserInform, on_delete=models.CASCADE, related_name='received_likes')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f'{self.sender.user.username} likes {self.receiver.user.username}'


class Friend(models.Model):
    """
    好友模型，用于表示用户之间的好友关系
    """
    user = models.ForeignKey(
        UserInform, related_name='user_friends', on_delete=models.CASCADE)
    friend = models.ForeignKey(
        UserInform, related_name='friend_of', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        """
        确保用户和好友的组合是唯一的
        """
        unique_together = ('user', 'friend')


class Message(models.Model):
    """
    消息模型，用于存储用户之间的私信内容
    """
    sender = models.ForeignKey(
        UserInform, on_delete=models.CASCADE, related_name='sent_messages')
    receiver = models.ForeignKey(
        UserInform, on_delete=models.CASCADE, related_name='received_messages')
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)


class Post(models.Model):
    """
    帖子模型，用于用户发布的内容
    """
    user = models.ForeignKey(UserInform, on_delete=models.CASCADE)
    content = models.TextField()
    image = models.ImageField(upload_to='posts/', blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
